package inge_soft.gestoriusNuevo.persistence.DAO.implementation;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import inge_soft.gestoriusNuevo.persistence.DAO.Exceptions.Abstraction.GestoriusException;
import inge_soft.gestoriusNuevo.persistence.DAO.abstraction.IPaisDAO;
import inge_soft.gestoriusNuevo.persistence.entity.implementation.Localidad;
import inge_soft.gestoriusNuevo.persistence.entity.implementation.Pais;
import inge_soft.gestoriusNuevo.persistence.entity.implementation.Provincia;

@Repository("paisDAO")
public class PaisDAO implements IPaisDAO {

	@PersistenceContext
	private EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Pais> obtenerTodosLosPaisesOrdenadosPorNombreNacionalidadAscendente() throws GestoriusException {
		return entityManager.createQuery("SELECT p FROM Pais p ORDER BY p.gentilicio ASC").getResultList();
	}

	@Transactional(propagation = Propagation.MANDATORY)
	public void crear(Pais pais) throws DataAccessException {
		entityManager.persist(pais);
	}

	@Transactional(propagation = Propagation.MANDATORY)
	public void modificar(Pais pais) throws GestoriusException {
		// TODO Auto-generated method stub
		
	}

	@Transactional(propagation = Propagation.MANDATORY)
	public void eliminar(Pais pais) throws GestoriusException {
		// TODO Auto-generated method stub
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Pais> obtenerTodosLosPaisesOrdenadosPorNombreAscendente() throws GestoriusException {
		return entityManager.createQuery("SELECT p FROM Pais p ORDER BY p.nombre ASC").getResultList();
	}
	
	@Transactional(readOnly = true)
	public List<Provincia> obtenerProvincias(Pais pais) throws GestoriusException {
		System.out.println("el dao recibio: "+pais.getClass() + "      id: " + pais.getId());
		return (entityManager.createQuery("SELECT p FROM Provincia p WHERE p.pais=:pais").setParameter("pais", pais)).getResultList();
	}
}
